// 02.数组去重
const arr = [ 1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {} ];
// => [1, '1', 17, true, false, 'true', 'a', {}, {}]
console.log(Array.from(new Set(arr)));

// 双重循环 + splice
const unique1 = (arr) => {
	let len = arr.length;
	for (let i = 0; i < len; i++) {
		for (let j = i + 1; j < len; j++) {
			if (arr[i] === arr[j]) {
				arr.splice(j, 1);
				// 每删除一个数，j--保证j的值经过自加后不变。同时，len--，减少循环次数提升性能
				len--;
				j--;
			}
		}
	}
	return arr;
};
console.log(unique1(arr));

// indexOf
const unique2 = (arr) => {
	const res = [];
	for (let i = 0; i < arr.length; i++) {
		if (res.indexOf(arr[i]) === -1) res.push(arr[i]);
	}
	return res;
};
console.log(unique2(arr));

// include
const unique3 = (arr) => {
	const res = [];
	for (let i = 0; i < arr.length; i++) {
		if (!res.includes(arr[i])) {
			res.push(arr[i]);
		}
	}
	return res;
};
console.log(unique3(arr));

// filter
const unique4 = (arr) => {
	return arr.filter((item, index) => {
		return arr.indexOf(item) === index;
	});
};

console.log(unique4(arr));

// Map
const unique5 = (arr) => {
	const map = new Map();
	const res = [];
	for (let i = 0; i < arr.length; i++) {
		if (!map.has(arr[i])) {
			map.set(arr[i], true);
			res.push(arr[i]);
		}
	}
	return res;
};
console.log(unique5(arr));
